package transactions.dq.policy;

import dinamica.Db;
import dinamica.GenericTableManager;
import dinamica.Recordset;
import dinamica.StringUtil;

/**
 * 指定分房政策(分多大房，各档的费用)<br>
 * <br>
 * (c) 2010 desoft.cn<br>
 * 
 * @author Anthony Wang (ghost2008@gmail.com)
 * */
public class PolicyPlacement extends GenericTableManager {

	public int service(Recordset inputParams) throws Throwable {
		int rc = super.service(inputParams);
		// 获取sql文件
		String delete = getSQL(getResource("delete.sql"), inputParams);
		String insert = getResource("insert.sql");

		// 获取页面数据集
		String is_local[] = getRequest().getParameterValues("is_local");
		String is_single[] = getRequest().getParameterValues("is_single");
		String single_status[] = getRequest().getParameterValues("single_status");
		String people_number[] = getRequest().getParameterValues("people_number");
		String house_number[] = getRequest().getParameterValues("house_number");
		String house_type_yingfen[] = getRequest().getParameterValues("house_type_yingfen");
		String house_type_kezeng[] = getRequest().getParameterValues("house_type_kezeng");
		/*
		String yingfen_area[] = getRequest().getParameterValues("yingfen_area");
		String kezeng_area[] = getRequest().getParameterValues("kezeng_area");
		String duiding_maxarea_zheng[] = getRequest().getParameterValues("duiding_maxarea_zheng");
		String duiding_price[] = getRequest().getParameterValues("duiding_price");
		String yingfen_price[] = getRequest().getParameterValues("yingfen_price");
		String zengdang_price[] = getRequest().getParameterValues("zengdang_price");
		String chaodang_price[] = getRequest().getParameterValues("chaodang_price");
		*/
		String remark[] = getRequest().getParameterValues("remark");

		Db db = getDb();
		// 先删除原数据，再重新插入新数据
		db.addBatchCommand(delete);

		// 必填项不能为空
		if (is_local == null || is_single == null || single_status == null || house_type_yingfen == null 
			|| house_type_kezeng == null || people_number == null || house_number == null) {
			throw new Throwable("数据填写不完整，请确认后再提交。");
		}
		int intDataLength = is_single.length;

		// 添加批处理插入语句
		for (int i = 0; i < intDataLength; i++) {
			if (is_local[i] == null || is_local[i].trim().length() == 0) {
				throw new Throwable("是否为坐地户不能为空，请确认后再提交。");
			}
			if (is_single[i] == null || is_single[i].trim().length() == 0) {
				throw new Throwable("是否为独生子女不能为空，请确认后再提交。");
			}
			if (single_status[i] == null || single_status[i].trim().length() == 0) {
				throw new Throwable("离婚丧偶情况不能为空，请确认后再提交。");
			}
			if (people_number[i] == null || people_number[i].trim().length() == 0) {
				throw new Throwable("家庭人口不能为空，请确认后再提交。");
			}
			if (house_type_yingfen[i] == null || house_type_yingfen[i].trim().length() == 0) {
				throw new Throwable("应分户型不能为空，请确认后再提交。");
			}
			if (house_type_kezeng[i] == null || house_type_kezeng[i].trim().length() == 0) {
				throw new Throwable("可增户型不能为空，请确认后再提交。");
			}
			if (house_number[i] == null || house_number[i].trim().length() == 0) {
				throw new Throwable("正房间数不能为空，请确认后再提交。");
			}
			/*
			if (duiding_maxarea_zheng[i] == null || duiding_maxarea_zheng[i].trim().length() == 0) {
				throw new Throwable("可兑顶最大面积不能为空，请确认后再提交。");
			}
			try {
				Double.valueOf(duiding_maxarea_zheng[i]);
			} catch (NumberFormatException e) {
				throw new Throwable("可兑顶最大面积格式不正确，请确认后再提交。");
			}	
			if (yingfen_area[i] == null || yingfen_area[i].trim().length() == 0) {
				throw new Throwable("应分面积能为空，请确认后再提交。");
			}
			try {
				Double.valueOf(yingfen_area[i]);
			} catch (NumberFormatException e) {
				throw new Throwable("应分面积格式不正确，请确认后再提交。");
			}
			if (kezeng_area[i] == null || kezeng_area[i].trim().length() == 0) {
				throw new Throwable("可增面积不能为空，请确认后再提交。");
			}
			try {
				Double.valueOf(kezeng_area[i]);
			} catch (NumberFormatException e) {
				throw new Throwable("可增面积格式不正确，请确认后再提交。");
			}

			if (duiding_price[i] == null || duiding_price[i].trim().length() == 0) {
				throw new Throwable("兑顶价格能为空，请确认后再提交。");
			}
			try {
				Double.valueOf(duiding_price[i]);
			} catch (NumberFormatException e) {
				throw new Throwable("兑顶价格格式不正确，请确认后再提交。");
			}
			if (yingfen_price[i] == null || yingfen_price[i].trim().length() == 0) {
				throw new Throwable("应分价格不能为空，请确认后再提交。");
			}
			try {
				Double.valueOf(yingfen_price[i]);
			} catch (NumberFormatException e) {
				throw new Throwable("应分价格格式不正确，请确认后再提交。");
			}

			if (zengdang_price[i] == null || zengdang_price[i].trim().length() == 0) {
				throw new Throwable("增档价格能为空，请确认后再提交。");
			}
			try {
				Double.valueOf(zengdang_price[i]);
			} catch (NumberFormatException e) {
				throw new Throwable("增档价格格式不正确，请确认后再提交。");
			}
			if (chaodang_price[i] == null || chaodang_price[i].trim().length() == 0) {
				throw new Throwable("超档价格不能为空，请确认后再提交。");
			}
			try {
				Double.valueOf(chaodang_price[i]);
			} catch (NumberFormatException e) {
				throw new Throwable("超档价格格式不正确，请确认后再提交。");
			}
			*/
			if (remark[i] != null && remark[i].trim().length() > 120) {
				throw new Throwable("备注输入太长，不能超过120个字符，请确认后再提交。");
			}
			String insertSql = "";
			insertSql = StringUtil.replace(insert, "${is_local}", is_local[i]);
			insertSql = StringUtil.replace(insertSql, "${is_single}", is_single[i]);
			insertSql = StringUtil.replace(insertSql, "${single_status}", single_status[i]);
			insertSql = StringUtil.replace(insertSql, "${people_number}", people_number[i]);
			insertSql = StringUtil.replace(insertSql, "${house_type_yingfen}", house_type_yingfen[i]);
			insertSql = StringUtil.replace(insertSql, "${house_type_kezeng}", house_type_kezeng[i]);
			insertSql = StringUtil.replace(insertSql, "${house_number}", house_number[i]);
			/*
			insertSql = StringUtil.replace(insertSql, "${duiding_maxarea_zheng}", duiding_maxarea_zheng[i]);
			insertSql = StringUtil.replace(insertSql, "${yingfen_area}", yingfen_area[i]);
			insertSql = StringUtil.replace(insertSql, "${kezeng_area}", kezeng_area[i]);
			insertSql = StringUtil.replace(insertSql, "${duiding_price}", duiding_price[i]);
			insertSql = StringUtil.replace(insertSql, "${yingfen_price}", yingfen_price[i]);
			insertSql = StringUtil.replace(insertSql, "${zengdang_price}", zengdang_price[i]);
			insertSql = StringUtil.replace(insertSql, "${chaodang_price}", chaodang_price[i]);
			*/
			insertSql = StringUtil.replace(insertSql, "${remark}", remark[i]);
			insertSql = getSQL(insertSql, inputParams);
			//System.out.println(insertSql);
			db.addBatchCommand(insertSql);
		}

		db.exec();
		return rc;
	}

}
